package com.example.demosecurityjwtlogin.security.services;




import com.example.demosecurityjwtlogin.models.Bean;

import com.example.demosecurityjwtlogin.repository.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BeanService {
    @Autowired
    BeanMapper beanMapper;
//    public int addBank(Bank bank){
//        return bankMapper.addBank(bank);
//    }
//    public int deleteBankById(int id){
//        return bankMapper.deleteBankById(id);
//    }
//    public int updateBankById(Bank bank){
//        return  bankMapper.updateBankById(bank);
//    }
//    public List<Bank> getAllBank(){
//        return bankMapper.getAllBank();
//    }
//    public Bank getCommentById(int id){
//        return bankMapper.findById(id);
//    }
public void addBook(Bean book){
    beanMapper.save(book);
}
    public void addAllBook(List<Bean> books){
        beanMapper.saveAll(books);
    }
    public List<Bean> getBook(){
        return beanMapper.findAll();
    }
    public Bean GetBooks(Long id){
        return beanMapper.findById(id).orElse(null);
    }
    public String putBook(Bean book){
        String result;
        if (beanMapper.existsById(book.getId()))
        {
            beanMapper.save(book);
            result="ok";
        }
        else result="Update failed";
        return result;
    }
    public String delete(Long id){
        String result;
        if (beanMapper.existsById(id))
        {
            beanMapper.deleteById(id);
            result="ok";
        }
        else result="not found";
        return result;
    }
    public List<Bean> getBooksByuserStartingWith(String user){
        return beanMapper.getBooksByuserStartingWith(user);
    }
    public List<Bean> getBooksBybalanceGreaterThan(Integer balance){
        return beanMapper.getBooksBybalanceGreaterThan(balance);
    }
    public Bean getMaxIdBook( ){
        return beanMapper.getMaxIdBook();
    }
    public List<Bean> getBooksByIdAndAuthor(String user, Long id){
        return beanMapper.getBooksByIdAndAuthor(user,id);
    }
    public List<Bean> getBooksByIdAndName(String user, Long id){
        return beanMapper.getBooksByIdAndName(user,id);
    }
    public List<Bean> getBookBypage (int pageNo, int size){
        Sort.TypedSort<Bean> book=Sort.sort(Bean.class);
        Sort sort=book.by(Bean::getCard_number).ascending();
        Pageable pageable = PageRequest.of(pageNo,size,sort);
        Page<Bean> books= beanMapper.findAll(pageable);
        return books.getContent();
    }
    public Page<Bean> getBookByPage (Pageable pageable){
        return beanMapper.findAll(pageable);
    }
}
